import numpy as np
import csv
from datetime import datetime
from time import strftime


def getCurTime():
    """
    get current time
    Return value of the date string format(%Y-%m-%d %H:%M:%S)
    """
    format='%Y-%m-%d %H:%M:%S'
    sdate = None
    cdate = datetime.now()
    try:
        sdate = cdate.strftime(format)
    except:
        raise ValueError
    return sdate

def build_data_list(inputCSV):
    sKey = []
    fn = inputCSV
    f = open(inputCSV)
    #ra = csv.DictReader(file(fn), dialect="excel")
    ra = csv.DictReader(f, dialect="excel")
    
    for record in ra:
        #print record[ra.fieldnames[0]], type(record[ra.fieldnames[-1]])
        for item in ra.fieldnames:
            temp = int(float(record[item]))
            sKey.append(temp)
    sKey = np.array(sKey)
    sKey.shape=(-1,len(ra.fieldnames))
    return sKey

#--------------------------------------------------------------------------
#MAIN

if __name__ == "__main__":
    print '===================================================='
    print "begin at " + getCurTime()

    filepath = 'C:/_DATA/migration_census_2000/'
    #filepath = 'C:/_DATA/migration89_08/COUNTY Migration/clean/test/US_census_pop_2011_county_id.csv'

    dataCSV = filepath + 'census_county_migration_format.csv'
    data = build_data_list(dataCSV)

    idtableCSV = filepath + 'Aggregation/cnty_aggr_min_100k_943reg.csv'
    idtable = build_data_list(idtableCSV)

    n = len(np.unique(idtable[:,-1]))
    print n

    flowtable = np.zeros((n,n))
    
    for item in data:
        oid = idtable[item[0],1]
        iid = idtable[item[1],1]
        flowtable[oid, iid] += item[2]

    output = []
    for i in range(len(flowtable)):
        for j in range(len(flowtable[:,0])):
            if (flowtable[i,j] > 0) and (i <> j):
                output.append([i,j,int(flowtable[i,j])])
        

    np.savetxt(filepath + 'census_county_migration_aggregation_1.csv', output, delimiter=',', fmt = '%s')